package com.wuyixu.controller;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wuyixu.domain.Brand;
import com.wuyixu.domain.Spu;
import com.wuyixu.domain.SpuVo;
import com.wuyixu.service.BrandService;
import com.wuyixu.service.SpuService;

@Controller
@RequestMapping("spu")
public class SpuController {
	@Reference
	private SpuService spuService;
	@Reference
	private BrandService brandService;
	@Autowired
	private FileUtil fileUtil;
	@RequestMapping("list")
	public String list(Model model,SpuVo spuVo){
		PageInfo<Spu> info = spuService.list(spuVo);
		model.addAttribute("info", info);
		model.addAttribute("spuVo", spuVo);
		List<Brand> list = brandService.queryAll();
		model.addAttribute("list", list);
		return "spu/list";
	}
	@RequestMapping("toAdd")
	public String toAdd(Model model){
		List<Brand> list = brandService.queryAll();
		model.addAttribute("list", list);
		return "spu/add";
	}
	@RequestMapping("add")
	@ResponseBody
	public String add(Spu spu,@RequestParam("myfile")MultipartFile file){
		String upload = fileUtil.upload(file);
		spu.setSmallPic(upload);
		
		return spuService.add(spu)>0?"ok":"failed";
	}
	@RequestMapping("del")
	@ResponseBody
	public String del(Spu spu,int[] ids){
		
		
		return spuService.delete(ids)>0?"ok":"failed";
	}
	@RequestMapping("toUpdate")
	public String toUpdate(Model model,int id){
		Spu spu = spuService.getById(id);
		List<Brand> list = brandService.queryAll();
		model.addAttribute("list", list);
		model.addAttribute("spu", spu);
		return "spu/update";
	}
	@RequestMapping("update")
	@ResponseBody
	public String update(Spu spu,@RequestParam("myfile")MultipartFile file){
		String upload = fileUtil.upload(file);
		spu.setSmallPic(upload);
		
		return spuService.update(spu)>0?"ok":"failed";
	}
}
